﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Com.Toook.Common
{
    public class StringHelper
    {
        /// <summary>
        /// 删除字符串str(str不能为null)中多余的空白，只留一个空白
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveMoreWhitespace(string str)
        {
            if (str == null)
            {
                return "";
            }
            string pattern = @"\s+";//匹配1到多个空白
            Regex rgx = new Regex(pattern);
            string result = rgx.Replace(str, " ");//str为null时正则表达式Replace会报错
            return result;
        }
        /// <summary>
        /// 删除从第一个开始字符串到后面“第一个IndexOf”结束字符串. 如：(A12_45_45G, 12, 45)结果(A_45G),删除12_45
        /// </summary>
        /// <param name="sb">待删除的字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否删除成功,删除成功的结果在sb中</returns>
        public static bool RemoveBetweenStart_FirstEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, true, MethodType.RemoveBetween, null);
        }
        /// <summary>
        /// 删除从第一个开始字符串到“最后一个LastIndexOf”结束字符串. 如：(A12c45M45G, 12, 45)结果(AG),删除12c45M45
        /// </summary>
        /// <param name="sb">待删除的字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否删除成功,删除成功的结果在sb中</returns>
        public static bool RemoveBetweenStart_LastEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, false, MethodType.RemoveBetween, null);
        }
        /// <summary>
        /// 删除从第一个开始字符串到后面“第一个IndexOf”结束字符串. 如：(A12_45_45G, 12, 45)结果(A_45G),删除12_45
        /// </summary>
        /// <param name="text">待删除的字符串文本</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>返回删除后的结果字符串</returns>
        public static string RemoveBetweenStart_FirstEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.RemoveBetween, true, null);
        }
        /// <summary>
        /// 删除从第一个开始字符串到“最后一个LastIndexOf”结束字符串. 如：(A12c45M45G, 12, 45)结果(AG),删除12c45M45
        /// </summary>
        /// <param name="text">待删除的字符串文本</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>返回删除后的结果字符串</returns>
        public static string RemoveBetweenStart_LastEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.RemoveBetween, false, null);
        }
        
        /// <summary>
        /// 用replaceText替换sb中从第一个开始字符串到后面“第一个IndexOf”结束字符串. 如：(A12_45_45G, -, 12, 45)结果(A-_45G)
        /// </summary>
        /// <param name="sb">待替换的字符串</param>
        /// <param name="replaceText">替换字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否成功,成功的结果在sb中</returns>
        public static bool ReplaceBetweenStart_FirstEnd(StringBuilder sb, string replaceText, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, true, MethodType.ReplaceBetween, replaceText);
        }
        /// <summary>
        /// 用replaceText替换sb中从第一个开始字符串到“最后一个LastIndexOf”结束字符串. 如：(A12_45_45G, -, 12, 45)结果(A-G)
        /// </summary>
        /// <param name="sb">待替换的字符串</param>
        /// <param name="replaceText">替换字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否成功,成功的结果在sb中</returns>
        public static bool ReplaceBetweenStart_LastEnd(StringBuilder sb, string replaceText, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, false, MethodType.ReplaceBetween, replaceText);
        }
        /// <summary>
        /// 用replaceText替换sb中从第一个开始字符串到后面“第一个IndexOf”结束字符串. 如：(A12_45_45G, -, 12, 45)结果(A-_45G)
        /// </summary>
        /// <param name="sb">待替换的字符串</param>
        /// <param name="replaceText">替换字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否成功,成功的结果在sb中</returns>
        public static string ReplaceBetweenStart_FirstEnd(string text, string replaceText, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.ReplaceBetween, true, replaceText);
        }
        /// <summary>
        /// 用replaceText替换sb中从第一个开始字符串到“最后一个LastIndexOf”结束字符串. 如：(A12_45_45G, -, 12, 45)结果(A-G)
        /// </summary>
        /// <param name="sb">待替换的字符串</param>
        /// <param name="replaceText">替换字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否成功,成功的结果在sb中</returns>
        public static string ReplaceBetweenStart_LastEnd(string text, string replaceText, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.ReplaceBetween, false, replaceText);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，包含startStr, endStr。如：abc123def, bc, de 结果：bc123de
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static bool GetBetweenStart_FirstEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, true, MethodType.GetBetween, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，包含startStr, endStr。如：abc123def, bc, de 结果：bc123de
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static bool GetBetweenStart_LastEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, false, MethodType.GetBetween, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，包含startStr, endStr。如：abc123def, bc, de 结果：bc123de
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string GetBetweenStart_FirstEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.GetBetween, true, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，包含startStr, endStr。如：abc123def, bc, de 结果：bc123de
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string GetBetweenStart_LastEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.GetBetween, false, null);            
        }

        /// <summary>
        /// 获取startStr, endStr之间的子串，不包含startStr, endStr。如：abc123def, bc, de 结果：123
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static bool GetSubstringNotContainStart_FirstEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, true, MethodType.GetSubstringNotContain, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，不包含startStr, endStr。如：abc123def, bc, de 结果：123
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static bool GetSubstringNotContainStart_LastEnd(StringBuilder sb, string startStr, string endStr)
        {
            return HandleRemoveOrReplace(sb, startStr, endStr, false, MethodType.GetSubstringNotContain, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，不包含startStr, endStr。如：abc123def, bc, de 结果：123
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string GetSubstringNotContainStart_FirstEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.GetSubstringNotContain, true, null);
        }
        /// <summary>
        /// 获取startStr, endStr之间的子串，不包含startStr, endStr。如：abc123def, bc, de 结果：123
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replaceText"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string GetSubstringNotContainStart_LastEnd(string text, string startStr, string endStr)
        {
            return GetTextResult(text, startStr, endStr, MethodType.GetSubstringNotContain, false, null);
        }

        /// <summary>
        /// 获取MethodType对应的文本结果
        /// </summary>
        /// <param name="text"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <param name="method"></param>
        /// <param name="isFirstEndPosition"></param>
        /// <param name="replaceText"></param>
        /// <returns></returns>
        private static string GetTextResult(string text, string startStr, string endStr, MethodType method, bool isFirstEndPosition, string replaceText)
        {
            StringBuilder sb = new StringBuilder(text);
            if (HandleRemoveOrReplace(sb, startStr, endStr, false, method, null))
            {
                return sb.ToString();
            }
            else
            {
                return text;//替换失败，返回原字符串
            }
        }
        /// <summary>
        /// 处理删除/替换/获取 开始/结束 字符串。
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <param name="isFirstEndPosition"></param>
        /// <param name="replaceText">删除传null, 替换传要替换的字符串</param>
        /// <returns></returns>
        private static bool HandleRemoveOrReplace(StringBuilder sb, string startStr, string endStr, bool isFirstEndPosition,MethodType method, string replaceText)
        {
            bool isOK = false;
            int startPosition = 0;
            int endPosition = 0;

            int start = sb.ToString().IndexOf(startStr);
            if (start == -1)
            {
                LogHelper.Log.Error("HandleRemoveOrReplace()没有找到开始标记:" + startStr + ", 文本:" + sb.ToString());
            }
            else
            {
                startPosition = start;
                //获取结束字符串位置(第一个/最后一个)
                int end = isFirstEndPosition ? sb.ToString().IndexOf(endStr, start) : sb.ToString().LastIndexOf(endStr);
                if (end == -1)
                {
                    LogHelper.Log.Error("HandleRemoveOrReplace()没有找到结束标记:" + endStr + ", 文本:" + sb.ToString());
                }
                else
                {
                    endPosition = end;
                    if (MethodType.RemoveBetween == method)
                    {
                        sb.Remove(startPosition, endPosition - startPosition + endStr.Length);
                    }
                    else if (MethodType.ReplaceBetween == method)
                    {
                        string oldStr = sb.ToString().Substring(startPosition, endPosition - startPosition + endStr.Length);
                        sb.Replace(oldStr, replaceText, startPosition, endPosition - startPosition + endStr.Length);
                    }
                    else if (MethodType.GetBetween == method)
                    {
                        string subStr = sb.ToString().Substring(startPosition, endPosition - startPosition + endStr.Length);
                        sb = new StringBuilder(subStr);
                    }
                    else if (MethodType.GetSubstringNotContain == method)
                    {
                        string subStr = sb.ToString().Substring(startPosition + startStr.Length, endPosition - startPosition - startStr.Length);
                        sb = new StringBuilder(subStr);
                    }
                    else
                    {
                        LogHelper.Log.Error("HandleRemoveOrReplace() MethodType错误 = " + method.ToString());
                    }
                    isOK = true;//操作成功
                }
            }
            return isOK;
        }
        
    }

    enum MethodType
    {
        /// <summary>
        /// 获取startStr, endStr之间的子串，包含startStr, endStr。如：abc123def, bc, de 结果：bc123de
        /// </summary>
        GetBetween,
        /// <summary>
        /// 获取startStr, endStr之间的子串，不包含startStr, endStr。如：abc123def, bc, de 结果：123
        /// </summary>
        GetSubstringNotContain,
        RemoveBetween,
        ReplaceBetween
    }
}
